package com.ljfiction.book.activitys;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;

import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.google.gson.Gson;
import com.google.gson.JsonSyntaxException;
import com.ljfiction.book.Baner.MuchDashangBean;
import com.ljfiction.book.MyView.DividerItemDecoration;
import com.ljfiction.book.R;
import com.ljfiction.book.http.HttpManager;
import com.ljfiction.book.tools.SettingManager;
import com.ljfiction.book.tools.TimeTool;
import com.lzy.okhttputils.OkHttpUtils;
import com.lzy.okhttputils.callback.StringCallback;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadMoreListener;
import com.zhy.autolayout.utils.AutoUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import jp.wasabeef.glide.transformations.CropCircleTransformation;
import okhttp3.Call;
import okhttp3.Response;

public class DashangbookdatasActivity extends BaseActivity {

    @BindView(R.id.dashangbookdatasback)
    ImageView dashangbookdatasback;
    @BindView(R.id.loading_bookdatasdashang)
    ProgressBar loadingBookdatasdashang;
    @BindView(R.id.recycle_bookdatasdashng)
    RecyclerView recycleBookdatasdashng;
    @BindView(R.id.refresh_dashngbookdatas)
    SmartRefreshLayout refreshDashngbookdatas;
    @BindView(R.id.dashangqutop)
    RelativeLayout dashangqutop;

    private BaseQuickAdapter mAdapter;
    private List<MuchDashangBean.DataBean.ListBean> datas = new ArrayList<>();
    private int page = 1;
    private String bookId;
    private MuchDashangBean muchDash;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_dashangbookdatas);
        ButterKnife.bind(this);
        bookId = getIntent().getStringExtra("bookId");
        dashangqutop.getBackground().setAlpha(255);
        intView();
        intData();
    }

    private void intData() {
        OkHttpUtils.post(HttpManager.IP + "/getSpicyirewardticketlogByBookId")

                .params("bookid", bookId)
                .params("startpage", String.valueOf(page))
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(String s, Call call, Response response) {
                        loadingBookdatasdashang.setVisibility(View.GONE);
                        refreshDashngbookdatas.finishRefresh();
                        refreshDashngbookdatas.finishLoadMore();
                        Gson g = new Gson();
                        try {
                            muchDash = g.fromJson(s, MuchDashangBean.class);
                            if (muchDash.getReturnCode() == 200) {
                                datas.addAll(muchDash.getData().getList());
                                mAdapter.notifyDataSetChanged();
                            }
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                });
    }

    private void intView() {
        mAdapter = new BaseQuickAdapter<MuchDashangBean.DataBean.ListBean, BaseViewHolder>(R.layout.bookdatas_dashang, datas) {
            @Override
            protected void convert(BaseViewHolder helper, MuchDashangBean.DataBean.ListBean item) {
                AutoUtils.autoSize(helper.itemView);
                helper.setText(R.id.username_dashangitem, item.getPseudonym())
                        .setText(R.id.dengji_dashangitem, "LV" + item.getUserGrade())
                        .setText(R.id.time_dashangitem, TimeTool.getTimeByLongStr(String.valueOf(item.getGiveDateTime())))
                        .setText(R.id.num_dasahngitem, String.valueOf(item.getSpicyiTicketCount()))
                        .setText(R.id.mansay_dashangitem, item.getArewardContent());
                if (item.getVipGrade() > 0) {
                    helper.getView(R.id.huangguan_dashangmuch).setVisibility(View.VISIBLE);
                } else {
                    helper.getView(R.id.huangguan_dashangmuch).setVisibility(View.GONE);
                }
                if (item.getUserSex() == 0) {
                    ((ImageView) helper.getView(R.id.sex_dashangitem)).setImageResource(R.mipmap.sex_n);
                } else {
                    ((ImageView) helper.getView(R.id.sex_dashangitem)).setImageResource(R.mipmap.sex_v);
                }
                Glide.with(mContext).load(item.getUserHeadPortraitURL()).bitmapTransform(new CropCircleTransformation(mContext)).into((ImageView) helper.getView(R.id.manimage_dashangitem));

            }
        };
        recycleBookdatasdashng.setAdapter(mAdapter);
        recycleBookdatasdashng.setLayoutManager(new LinearLayoutManager(this));
        recycleBookdatasdashng.addItemDecoration(new DividerItemDecoration(this, DividerItemDecoration.VERTICAL_LIST));
        refreshDashngbookdatas.setOnRefreshLoadMoreListener(new OnRefreshLoadMoreListener() {
            @Override
            public void onLoadMore(RefreshLayout refreshlayout) {
                page++;
                intData();
            }

            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                page = 1;
                datas.clear();
                intData();
            }
        });
    }

    @OnClick(R.id.dashangbookdatasback)
    public void onViewClicked() {
        goBack();
    }
}
